//
// MixupPropsCipher
//
// Copyright (C) jextra.net.
//
//  This file is part of the JavaProps Library.
//
//  The JavaProps Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The JavaProps Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the JavaProps Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.props;

import java.util.*;

/**
 * <p>
 * A very simply cipher that has a map of characters to other "mixed up" characters.
 * </p><p>
 * This implementation was intentionally made simple in order to avoid any dependencies on external 
 * libraries.
 * </p>
 */
public class MixupPropsCipher implements PropsCipher
{
    // ============================================================
    // Fields
    // ============================================================

    private static String IN =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*();:/?\\|`~-_=+";
    private static String OUT =
        "N9ko-rbO%n_08Gw=mP!Czc#1KaFBR;JdS~`:pEutvMLqADs6lI/^\\?&fVZ+jH3h7Q5*Ugi|)(YT$xy@24eXW";

    private HashMap<Character,Character> _encryptMap;
    private HashMap<Character,Character> _decryptMap;

    // ============================================================
    // Constructors
    // ============================================================

    public MixupPropsCipher()
    {
        _encryptMap = new HashMap<Character,Character>();
        _decryptMap = new HashMap<Character,Character>();
        for ( int i = 0; i < IN.length(); i++ )
        {
            if ( _encryptMap.get( IN.charAt( i ) ) != null )
            {
                throw new RuntimeException( "IN String is bad (" + i + ")" );
            }

            if ( _decryptMap.get( OUT.charAt( i ) ) != null )
            {
                throw new RuntimeException( "OUT String is bad (" + i + ")" );
            }

            _encryptMap.put( IN.charAt( i ), OUT.charAt( i ) );
            _decryptMap.put( OUT.charAt( i ), IN.charAt( i ) );
        }
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public static void main( String[] args )
        throws Exception
    {
        try
        {
            MixupPropsCipher cipher = new MixupPropsCipher();
            System.out.println( "Encrypted String: " + cipher.encipher( args[0] ) );
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
            throw ex;
        }
    }


    public String encipher( String string )
    {
        if ( string == null )
        {
            return null;
        }

        StringBuilder builder = new StringBuilder();
        for ( int i = 0; i < string.length(); i++ )
        {
            char c = string.charAt( i );
            Character k = _encryptMap.get( c );
            builder.append( k == null ? c : k );
        }

        return builder.toString();
    }

    public String decipher( String string )
    {
        if ( string == null )
        {
            return null;
        }

        StringBuilder builder = new StringBuilder();
        for ( int i = 0; i < string.length(); i++ )
        {
            char c = string.charAt( i );
            Character k = _decryptMap.get( c );
            builder.append( k == null ? c : k );
        }

        return builder.toString();
    }
}

